<template>
  <el-dialog
    :title="!this.dataForm.adId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
    >
      <el-form-item label="详情地址" prop="adAddress">
        <el-input
          v-model="dataForm.adAddress"
          placeholder="详情地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="adPhone">
        <el-input v-model="dataForm.adPhone" placeholder="手机号"></el-input>
      </el-form-item>

      <el-form-item label="地址" prop="adProvince">
          <el-select v-model="dataForm.adProvince" placeholder="请选择"    @change="changeSheng">
          <el-option
            v-for="item in sheng"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
     <el-select style="left:40px;" v-model="dataForm.adCity" placeholder="请选择"    @change="changeSheng">
          <el-option
            v-for="item in eName"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <!-- <el-input v-model="dataForm.adProvince" placeholder="省"></el-input> -->
      </el-form-item>     
      <!-- <el-form-item label="用户id" prop="adUrId">
        <el-input v-model="dataForm.adUrId" placeholder="用户id"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from "@/api/modules/cbbaddress";
import area from "@/api/modules/cbbarea";
export default {
  data() {
    return {
      citys:[],
      sheng: [],
      eName: [],
      visible: false,
      dataForm: {
        adId: 0,
        adAddress: "",
        adPhone: "",
        adCity: "",
        adProvince: "",
        adUrId: "",
      },
      dataRule: {
        adAddress: [
          { required: true, message: "详情地址不能为空", trigger: "blur" },
        ],
        adPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        adCity: [{ required: true, message: "城市不能为空", trigger: "blur" }],
        adProvince: [
          { required: true, message: "省不能为空", trigger: "blur" },
        ],
        adUrId: [
          { required: true, message: "用户id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    changeSheng(v){
      this.eName=this.citys[v]
      this.dataForm.adCity=this.eName[0]
    },
    listarea() {
      area.list().then(({ data }) => {
        data = data.citys;
        var city = JSON.parse(data);
        this.citys=city
        for (const key in city) {
          this.sheng.push(key);
        }
        //console.log(city[this.dataForm.adProvince],this.dataForm.adProvince)
        this.eName=city[this.dataForm.adProvince]

        //   console.log(this.sheng)
        //  console.log(this.eName)
      });
    },
    init(id) {
      this.dataForm.adId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        api.info(this.dataForm.adId).then(({ data }) => {
          if (data) {
            this.dataForm.adAddress = data.cbbAddress.adAddress;
            this.dataForm.adPhone = data.cbbAddress.adPhone;
            this.dataForm.adCity = data.cbbAddress.adCity;
            this.dataForm.adProvince = data.cbbAddress.adProvince;
            this.dataForm.adUrId = data.cbbAddress.adUrId;
                this.listarea();
         }
        });
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          var parms = {
            adId: this.dataForm.adId || undefined,
            adAddress: this.dataForm.adAddress,
            adPhone: this.dataForm.adPhone,
            adCity: this.dataForm.adCity,
            adProvince: this.dataForm.adProvince,
            adUrId: this.dataForm.adUrId,
          };
          api.updateAdd(parms, this.dataForm.adId).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
